home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
STANDALO
/
BOUNCE_C
/
LINEBOX.C
< prev
next >
Wrap
Text File
|
1991-01-18
|
3KB
|
161 lines
/*
* LineBox.c -- implementation of the LineBox class.
*/
#include "LineBox.h"
#include <QuickDraw.h>
#include <MemoryMgr.h>
/*
* randseed for my random number generator
*/
static unsigned long randseed = 1;
/*
* Initialize object and return FALSE if NewPtr call fails.
*/
Boolean LineBox::Init(int nlines, Rect *bounceBox, GrafPtr savePort)
{
Rect bounds = *bounceBox;
int i;
dtop = 3;
dbottom = 3;
dleft = 2;
dright = 6;
numlines = nlines;
port = savePort;
top = bounds.top;
bottom = bounds.bottom;
left = bounds.left;
right = bounds.right;
GetDateTime((long *)&randseed);
if (lines = (Rect *)NewPtr((unsigned long)sizeof(Rect) * nlines)) {
RandomRect(&(lines[0]));
for (i = 1; i < numlines; ++i) {
lines[i] = lines[i - 1];
RecalcLine(i);
}
return TRUE;
}
else
return FALSE;
}
void LineBox::DrawLine(int i)
{
MoveTo(lines[i].left, lines[i].top);
LineTo(lines[i].right, lines[i].bottom);
}
static int Randomize(int range)
{
long rawResult;
rawResult = rand();
if (rawResult < 0)
rawResult = - rawResult;
return (rawResult * range) / 32768;
}
void LineBox::RandomRect(Rect *myRectPtr)
{
myRectPtr->left = Randomize(right - left) + left;
myRectPtr->right = Randomize(right - left) + left;
myRectPtr->top = Randomize(bottom - top) + top;
myRectPtr->bottom = Randomize(bottom - top) + top;
}
void LineBox::RecalcLine(int i)
{
lines[i].top += dtop;
if ((lines[i].top < top) || (lines[i].top > bottom)) {
dtop *= -1;
lines[i].top += 2 * dtop;
}
lines[i].bottom += dbottom;
if ((lines[i].bottom < top) || (lines[i].bottom > bottom)) {
dbottom *= -1;
lines[i].bottom += 2 * dbottom;
}
lines[i].left += dleft;
if ((lines[i].left < left) || (lines[i].left > right)) {
dleft *= -1;
lines[i].left += 2 * dleft;
}
lines[i].right += dright;
if ((lines[i].right < left) || (lines[i].right > right)) {
dright *= -1;
lines[i].right += 2 * dright;
}
}
void LineBox::Draw()
{
Rect frame;
PenState savePen;
GrafPtr savePort;
int i;
GetPort(&savePort);
SetPort(port);
GetPenState(&savePen);
PenNormal();
SetRect(&frame, left, top, right, bottom);
InsetRect(&frame, -BORDER, -BORDER);
/* was: FillRect(&frame, &black); black is an A5 qd global, no can do in cdevs */
EraseRect(&frame);
InvertRect(&frame);
PenMode(patXor);
for (i = 0; i < numlines; ++i) {
DrawLine(i);
}
SetPenState(&savePen);
SetPort(savePort);
}
void LineBox::Idle()
{
GrafPtr savePort;
PenState savePen;
int i;
GetPort(&savePort);
SetPort(port);
GetPenState(&savePen);
PenMode(patXor);
DrawLine(numlines - 1);
for (i = numlines - 1; i > 0; --i)
lines[i] = lines[i-1];
RecalcLine(0);
DrawLine(0);
SetPenState(&savePen);
SetPort(savePort);
}
void LineBox::Close()
{
if (lines)
DisposPtr(lines);
}
/*
* rand - pseudo-random number generator
* (from THINK C 4.0 ANSI library)
*/
static int
rand()
{
randseed = randseed * 1103515245 + 12345;
asm {
move.w randseed,d0 ; high word of long
andi.w #0x7FFF,d0 ; remove high bit
}
}